#include "producedata.h"
#include "ui_producedata.h"
#include <cstdio>
#include <vector>
#include <iostream>
#include <sstream>
#include <QDebug>

using namespace std;

ProduceData::ProduceData(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ProduceData), wordIndex(0), wordNum(0), charIndex(0), outputFile(nullptr), totalChar(0), curChar(0)
{
    ui->setupUi(this);
    m_view = new PadGraphicsView;
    ui->verticalLayout->addWidget(m_view);
    initText();
    dataKeyboard = new DataKeyboard;
    connect(dataKeyboard, SIGNAL(keyTapped(float, float)), this, SLOT(receiveOneTap(float, float)));
    connect(dataKeyboard, SIGNAL(keyTapped(float, float)), m_view, SLOT(acceptOneTap(float, float)));
    dataKeyboard->start();
}

static vector<string> &split(const string &str, char delim, vector<string> &elems, bool skip_empty = true) {
    istringstream iss(str);
    for (string item; getline(iss, item, delim); )
        if (skip_empty && item.empty()) continue;
        else elems.push_back(item);
    return elems;
}


void ProduceData::initText()
{
    FILE* fp = fopen("/Users/fanqu/programs/cv/data/textdata0/datatext1.txt", "r");
    outputFile = fopen("/Users/fanqu/programs/cv/data/textdata0/train-data.txt", "w");
    if(fp == NULL || outputFile == nullptr)
        throw "Can not open datatext";
    else {
        fgets(this->text, max_text_len, fp);
        split(string(this->text), ' ', this->words);
        wordNum = this->words.size();
        wordIndex = 0;
        lastWordIndex = 0;
        totalChar = 0;
        curChar = 0;
        for(int i = 0; i < words.size(); ++i)
            totalChar += words[i].length();
    }
    showWords();

}

void ProduceData::appendData(cv::Point2f point, char ch)
{
    fprintf(outputFile, "%.2f %.2f %d\n", point.x, point.y, ch - 96);
}

void ProduceData::showWords()
{
    size_t remainNum = wordNum - wordIndex;
    std::string showText;
    for(size_t i = 0; i < min(remainNum, (size_t)6); ++i) {
        showText += words[wordIndex+i] + " ";
        lastWordIndex = wordIndex + i;
    }
    ui->label1->setText(showText.c_str());
    ui->label2->setText(underStr.c_str());
    updateChar();
}

void ProduceData::updateChar()
{
    std::string labelString = std::to_string(curChar) + "/" + std::to_string(totalChar);
    ui->charNumLabel->setText(labelString.c_str());
}

void ProduceData::receiveOneTap(float x, float y)
{
    qDebug() << "receive Tap";
    if(wordIndex < wordNum) {
        underStr += "*";
        curChar++;
        appendData(cv::Point2f(x, y), words[wordIndex][charIndex]);
        charIndex++;
        updateChar();
        if(charIndex == words[wordIndex].length()) {
            wordIndex++;
            charIndex = 0;
            if(wordIndex == wordNum) {
                dataKeyboard->requestInterruption();
            }
            else {
                if(wordIndex == lastWordIndex + 1) {
                    underStr = "";
                    showWords();
                }
                else {
                    underStr += " ";
                }
            }
        }
        ui->label2->setText(underStr.c_str());
    }
}

ProduceData::~ProduceData()
{
    fclose(outputFile);
    delete ui;
}
